﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class FrmCapnhatquyetdinhkyluat : PRE.Catalog.FrmBase
    {
        #region Variable
        DAL.EntitiesDAL.KyluatDAL _dal = new DAL.EntitiesDAL.KyluatDAL();
        BLL.EntitiesBLL.KyluatBLL _bll = new BLL.EntitiesBLL.KyluatBLL();

        DataTable _dtb = new DataTable();
        #endregion

        #region Inits
        public FrmCapnhatquyetdinhkyluat()
        {
            InitializeComponent();
        }
        #endregion

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            DataTable _ma = _dal.Select_Max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                    txt_Sttquyetdinh.Text = ma;

                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);

                    dte_Ngayra.EditValue = DateTime.Now;
                    txt_Lydo.ResetText();
                    txt_Noira.ResetText();

                    txt_Sttquyetdinh.Properties.ReadOnly = true;

                    txt_Lydo.Focus();
                }
           
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            bto_Themnhanvien.Visible = false;
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = grvKyluat.GetFocusedRowCellValue("STT_QDKL") + "";

            DialogResult result;
            var ok = _dal.Delete(id1);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txt_Sttquyetdinh.Properties.ReadOnly = true;
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();
            bto_Themnhanvien.Visible = false;
            bto_Search.Visible = false;

            if (_dtb != null)
            {
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
           
            var name_Lydo = Proper(txt_Lydo.Text);
            var name_Noira = Proper(txt_Noira.Text);
            if (IsAdd)
            {
                
                var o = new DAL.Entities.Kyluat()
                {
                    STT_QDKL = Convert.ToString(txt_Sttquyetdinh.Text),
                    LYDO_KL = name_Lydo,
                    NOIRA_QDKL = name_Noira,
                    NGAYRA_QDKL = Convert.ToDateTime(dte_Ngayra.EditValue)

                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    bto_Themnhanvien.Visible = true;
                    // PerformAdd();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var id1 = grvKyluat.GetFocusedRowCellValue("STT_QDKL") + "";


                var o = new DAL.Entities.Kyluat()
                {
                    STT_QDKL = Convert.ToString(txt_Sttquyetdinh.Text),
                    LYDO_KL = name_Lydo,
                    NOIRA_QDKL = name_Noira,
                    NGAYRA_QDKL = Convert.ToDateTime(dte_Ngayra.EditValue)

                };

                var oki = _dal.Update(o);

                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txt_Sttquyetdinh.ResetText();
            txt_Lydo.ResetText();
            txt_Noira.ResetText();

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txt_Sttquyetdinh.DataBindings.Clear();
            txt_Lydo.DataBindings.Clear();
            txt_Noira.DataBindings.Clear();
            dte_Ngayra.DataBindings.Clear();
            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            txt_Sttquyetdinh.DataBindings.Add("Text", _dtb, ".STT_QDKL");
            txt_Lydo.DataBindings.Add("Text", _dtb, ".LYDO_KL");
            txt_Noira.DataBindings.Add("Text", _dtb, ".NOIRA_QDKL");
            dte_Ngayra.DataBindings.Add("EditValue", _dtb, ".NGAYRA_QDKL");
            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {

            txt_Sttquyetdinh.Properties.ReadOnly = isReadOnly;
            txt_Lydo.Properties.ReadOnly = isReadOnly;
            txt_Noira.Properties.ReadOnly = isReadOnly;
            dte_Ngayra.Properties.ReadOnly = isReadOnly;
            grcKyluat.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
           // ResetText();
            txt_Sttquyetdinh.ResetText();
            txt_Lydo.ResetText();
            txt_Noira.ResetText();
            bto_Search.Visible = true;

            grcKyluat.Enabled = true;

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

       private void bto_Search_Click_1(object sender, EventArgs e)
        {
            var name_Lydo = Proper(txt_Lydo.Text);
            var name_Noira = Proper(txt_Noira.Text);
            var o = new DAL.Entities.Kyluat()
            {
                STT_QDKL = Convert.ToString(txt_Sttquyetdinh.Text),
                LYDO_KL = name_Lydo,
                NOIRA_QDKL = name_Noira

            };

            grcKyluat.DataSource = _dal.Search(o);
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            try
            {
                _dtb = _dal.Select();

                if (_dtb != null)
                {
                    grcKyluat.DataSource = _dtb;

                    AppGridView.SummaryField(grvKyluat, "STT_QDKL", "Tổng = {0:#,0} Quyết định", DevExpress.Data.SummaryItemType.Count);

                    grvKyluat.OptionsView.ColumnAutoWidth = true;

                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Lỗi : " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.LoadData();
        }
        #endregion

        #region Form Load
        private void grcKyluat_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }
        #endregion

        #region Thêm nhân viên
        private void bto_Themnhanvien_Click(object sender, EventArgs e)
        {
            var frm = new Catalog.FrmKyluatnhanvien();
            frm.Show();
        }
        #endregion

    }
}
